using UnityEngine;

public class TableTennisMatchControllerIntro : TableTennisMatchController
{
	public static TableTennisMatchControllerIntro Create(TableTennisMatchController match)
	{
		TableTennisMatchControllerIntro tableTennisMatchControllerIntro = match.starter.CreateGameObject<TableTennisMatchControllerIntro>();
		tableTennisMatchControllerIntro.Init(match.starter, match.ui, match.gameType);
		AddIntroPlayer(match.GetPlayer(0), tableTennisMatchControllerIntro);
		AddIntroPlayer(match.GetPlayer(1), tableTennisMatchControllerIntro);
		return tableTennisMatchControllerIntro;
	}

	public static TableTennisIntroPlayer AddIntroPlayer(TableTennisPlayerBase player, TableTennisMatchControllerIntro match)
	{
		TableTennisIntroPlayer tableTennisIntroPlayer = player.GetComponent<TableTennisIntroPlayer>();
		if (tableTennisIntroPlayer == null)
		{
			tableTennisIntroPlayer = player.gameObject.AddComponent<TableTennisIntroPlayer>();
		}
		TableTennisAIPlayer tableTennisAIPlayer = player as TableTennisAIPlayer;
		TableTennisHumanPlayer tableTennisHumanPlayer = player as TableTennisHumanPlayer;
		TableTennisRemotePlayer tableTennisRemotePlayer = player as TableTennisRemotePlayer;
		Debug.Log("TABLE SIDE " + player.tableSide);
		if (tableTennisAIPlayer != null)
		{
			tableTennisIntroPlayer.aiDefinition = tableTennisAIPlayer.aiDefinition;
			tableTennisIntroPlayer.serveOffsetPosition = tableTennisAIPlayer.serveOffsetPosition;
		}
		else if (tableTennisHumanPlayer != null)
		{
			tableTennisIntroPlayer.aiDefinition = new PlayerDB.AiPlayerDefinition
			{
				index = 0
			};
			tableTennisIntroPlayer.serveOffsetPosition = tableTennisHumanPlayer.serveOffsetPosition;
		}
		else if (tableTennisRemotePlayer != null)
		{
			tableTennisIntroPlayer.aiDefinition = new PlayerDB.AiPlayerDefinition
			{
				index = 0
			};
			tableTennisIntroPlayer.serveOffsetPosition = tableTennisRemotePlayer.serveOffsetPosition;
		}
		tableTennisIntroPlayer.audioSources.AddRange(player.audioSources);
		tableTennisIntroPlayer.serveHand = player.serveHand;
		tableTennisIntroPlayer.animController = player.animController;
		tableTennisIntroPlayer.Init(player.teamTag, player.tableSide, match, true);
		tableTennisIntroPlayer.Reset();
		match.AddPlayer(tableTennisIntroPlayer);
		return tableTennisIntroPlayer;
	}

	public override void Init(TableTennisGameStartScript starter, MatchUI ui, TableTennisGameTypeBase gameType)
	{
		base.starter = starter;
		base.ball = CreateBall();
		GGGameTime.gameTimescale = 1f;
		base.gameType = gameType;
		base.ui = ui;
		Debug.Log("Init started");
	}

	public void AddPlayer(TableTennisIntroPlayer player)
	{
		players.Add(player);
	}

	public override void PlayerWillMissShot(TableTennisPlayerBase player)
	{
		if (players.Count > 1)
		{
			Other(player).OpponentWillMissShot(player);
		}
	}

	public override void StartGame()
	{
	}

	public override void OnPlayerChosenHitPlan(TableTennisPlayerBase player, PlayerAnimationsController.HitPlan hitPlan)
	{
		if (players.Count > 1)
		{
			Other(player).OpponentChosenHitPlan(player, hitPlan);
		}
	}

	public override void StartServe(TableTennisPlayerBase player)
	{
		base.ball.isBallInGame = false;
		player.StartServe(base.ball);
		if (players.Count > 1)
		{
			Other(player).AwaitServe();
		}
	}

	public override void OnPlayerHitBall(TableTennisPlayerBase player, HitBallParams hitBallParams)
	{
		base.gameType.OnPlayerHitBall(player, new HitBallParamsPhysicsFlight
		{
			missWide = false
		});
		if (players.Count > 1)
		{
			Other(player).ReceiveBall(base.ball);
		}
	}

	public override void OnPlayerHitBall(TableTennisPlayerBase player, HitBallParamsPhysicsFlight hitBallParams)
	{
		base.gameType.OnPlayerHitBall(player, hitBallParams);
		if (players.Count > 1)
		{
			Other(player).ReceiveBall(base.ball);
		}
	}

	public override void OnMissedServe()
	{
		StartServe(GetPlayer(0));
	}

	protected override void OnBallHitObject(PhysicsBall ball, Vector3 pos, Vector3 vel, HitObjectTag hitObjectTag)
	{
		if (hitObjectTag != HitObjectTag.None && hitObjectTag == HitObjectTag.Floor)
		{
			AwardPoint(default(TableTennisGameTypeBase.AwardPointParams));
		}
	}

	public override void AwardPoint(TableTennisGameTypeBase.AwardPointParams awardPointParams)
	{
		foreach (TableTennisPlayerBase player in players)
		{
			player.BallRemovedFromGame();
		}
		StartServe(GetPlayer(0));
	}

	public override void OnSwipe(TableTennisPlayerBase player)
	{
	}

	public override void OnGameWon(bool isPlayerWinner)
	{
	}

	public override void OnHitPlanAdded(SwipeToHitParams swipeParams)
	{
	}

	public override void OnStartServeToss()
	{
	}
}
